package com.ucs.daoDbImpl;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;


public class DBUtil {
	private static final Map<String, BasicDataSource> bdss = new HashMap<String, BasicDataSource>();
	static {
		bdss.put("ucs_admin", getBasicDataSource("ucs_admin"));
		bdss.put("ucs_food", getBasicDataSource("ucs_food"));
		bdss.put("ucs_order", getBasicDataSource("ucs_order"));
		System.out.println(bdss);
	}
	
	private static BasicDataSource getBasicDataSource(String dbName){
		BasicDataSource bds= new BasicDataSource();
		bds.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
		bds.setUrl("jdbc:jtds:sqlserver://localhost:1433/"+dbName);
		bds.setUsername("sa");
		bds.setPassword("");
		bds.setInitialSize(2);
		bds.setMaxActive(5);
		return bds;
	}
	
	public static Connection getDbConnection(String dbName) {
		try
		{
			return bdss.get(dbName).getConnection();			
		}
		catch (Exception e) {
			throw new UCSException("could not get db connetion for: "+dbName, e);
		}
	}

	public static void retConnection(Connection c){
		try {
			c.close();
		} catch (Exception e) {
			throw new UCSException("could not close db connetion", e);
		}
		
	}
	
	public static void main(String[] args) {
		
	}
}
